package com.heaboy.feedback.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.heaboy.feedback.domain.Clazz;
import com.heaboy.feedback.service.IClazzService;
import com.ruoyi.common.constant.RoleConstants;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.heaboy.feedback.mapper.StudentClazzMapper;
import com.heaboy.feedback.domain.StudentClazz;
import com.heaboy.feedback.service.IStudentClazzService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 学生Service业务层处理
 * 
 * @author heaboy
 * @date 2024-02-01
 */
@Service
public class StudentClazzServiceImpl extends ServiceImpl<StudentClazzMapper, StudentClazz> implements IStudentClazzService
{
    @Autowired
    private StudentClazzMapper studentClazzMapper;
    @Autowired
    private IClazzService clazzService;
    @Autowired
    private ISysRoleService sysRoleService;

    /**
     * 查询学生
     * 
     * @param id 学生主键
     * @return 学生
     */
    @Override
    public StudentClazz selectStudentClazzById(Long id)
    {
        return studentClazzMapper.selectStudentClazzById(id);
    }

    /**
     * 查询学生列表
     * 
     * @param studentClazz 学生
     * @return 学生
     */
    @Override
    public List<StudentClazz> selectStudentClazzList(StudentClazz studentClazz)
    {
        return studentClazzMapper.selectStudentClazzList(studentClazz);
    }

    /**
     * 新增学生
     * 
     * @param studentClazz 学生
     * @return 结果
     */
    @Override
    public int insertStudentClazz(StudentClazz studentClazz)
    {
        studentClazz.setCreateTime(DateUtils.getNowDate());
        return studentClazzMapper.insertStudentClazz(studentClazz);
    }

    /**
     * 修改学生
     * 
     * @param studentClazz 学生
     * @return 结果
     */
    @Override
    public int updateStudentClazz(StudentClazz studentClazz)
    {
        studentClazz.setUpdateTime(DateUtils.getNowDate());
        return studentClazzMapper.updateStudentClazz(studentClazz);
    }

    /**
     * 批量删除学生
     * 
     * @param ids 需要删除的学生主键
     * @return 结果
     */
    @Override
    public int deleteStudentClazzByIds(Long[] ids)
    {
        return studentClazzMapper.deleteStudentClazzByIds(ids);
    }

    /**
     * 删除学生信息
     * 
     * @param id 学生主键
     * @return 结果
     */
    @Override
    public int deleteStudentClazzById(Long id)
    {
        return studentClazzMapper.deleteStudentClazzById(id);
    }

    @Transactional
    @Override
    public boolean joinClazz(Clazz clazz) {
        Long userId = SecurityUtils.getUserId();
        Clazz one = clazzService.getOne(new LambdaUpdateWrapper<Clazz>().eq(Clazz::getClazzCode, clazz.getClazzCode().trim()));
        if(one==null){
            throw new ServiceException("当前班级码无对应班级，请确认班级码是否正确"+clazz.getClazzCode());
        }
        StudentClazz studentClazz = new StudentClazz().setClazzId(one.getId()).setStudentId(userId);
        List<SysRole> sysRoles = sysRoleService.selectRolesByUserId(userId);
        boolean hasStudent = sysRoles.stream().anyMatch(r -> r.getRoleId().equals(RoleConstants.ID_ROLE_STUDENT));
        if(!hasStudent){
            sysRoleService.insertAuthUsers(RoleConstants.ID_ROLE_STUDENT,new Long[]{userId});
        }
        StudentClazz selectOne = baseMapper.selectOne(new LambdaUpdateWrapper<StudentClazz>().eq(StudentClazz::getClazzId, one.getId()).eq(StudentClazz::getStudentId, userId));
        if(selectOne!=null){
            throw new ServiceException("您已经加入过该班级，无需重复加入"+clazz.getClazzCode()+" "+userId);
        }
        return baseMapper.insertStudentClazz(studentClazz)>0;
    }

    @Override
    public List<StudentClazz> listByClazzId(Long[] clazzIds) {
        return baseMapper.selectList(new LambdaUpdateWrapper<StudentClazz>().in(StudentClazz::getClazzId,clazzIds));
    }
}
